import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { repair_list, repair_delete, repair_add } from '../utils/api'

//局校通知
export const useRepairListStore = defineStore('repairlist', () => {
    const tableData = ref([])

    //保存tabledata数据
    function save_tableData() {
        repair_list().then((res) => {
            console.log('请求成功')
            console.log(res.data.list)
            tableData.value = res.data.list
        })
    }

    //删除数据
    function del_tableData(id) {
        repair_delete({ id }).then((res) => {
            console.log(res.data)
        })
        let i = tableData.value.findIndex((element) => {
            return element.id == id
        })
        tableData.value.splice(i, 1)
    }

    //添加数据
    function add_tableData(form) {
        let newdata = {}
        console.log(form)
        newdata.id = tableData.value.length + 1
        newdata.accepter = form.accepter
        newdata.content = form.desc
        newdata.name = form.name
        newdata.status = '待受理'
        //日期拼接
        let data = form.date1.getFullYear() + '-' + form.date1.getMonth() + '-' + form.date1.getDay() + ' ' + form.date2.getHours() + ':' + form.date2.getMinutes() + ':' + form.date2.getSeconds()
        newdata.data = data
        //将新增的通知加入到列表中
        tableData.value.push(newdata)
        console.log('新增维修', tableData.value)
        //向服务器发请求
        repair_add(newdata).then((res) => {
            if (res.data.code == 200) {
                console.log(res.data)
            }
        })

    }

    return { tableData, save_tableData, del_tableData, add_tableData }
})
